/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.navid.trafalgar.model.opengl2;

import com.jme3.asset.AssetManager;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.collision.CollisionResults;
import com.jme3.material.Material;
import com.jme3.math.Vector3f;
import com.jme3.scene.Spatial;
import com.navid.trafalgar.model.AMillestoneModel;
import com.navid.trafalgar.model.AShipModel;

/**
 *
 * @author alberto
 */
public class MillestoneModel extends AMillestoneModel {

    Material materialOn;
    Material materialOff;
    private RigidBodyControl millestone;
    boolean state = false;
    private Spatial spatial;

    public MillestoneModel(AssetManager assetManager) {
        super();
        spatial = assetManager.loadModel("Models/m1/m1.j3o");
        spatial.scale(3);
        this.attachChild(spatial);

        CollisionShape sceneShape =
                CollisionShapeFactory.createMeshShape(this);
        millestone = new RigidBodyControl(sceneShape, 0);
        this.addControl(millestone);
        millestone.setPhysicsLocation(new Vector3f(-200, 0, 100));
    }

    public void setMaterialOff(Material mat) {
        this.materialOff = mat;
    }

    public void setMaterialOn(Material mat) {
        this.materialOn = mat;
    }
    
    public boolean getState(){
        return state;
    }
    
    @Override
    public void setLocalTranslation(Vector3f pos){
        super.setLocalTranslation(pos);
        millestone.setPhysicsLocation(pos);
    }

    @Override
    public void update(float tpf) {
        
    }

    @Override
    public void updateState(AShipModel ship) {
        CollisionResults results = new CollisionResults();
        ship.collideWith(this.getWorldBound(), results);
        if (state || results.size() > 0) {
            this.setMaterial(materialOn);
            state = true;
        } else {
            this.setMaterial(materialOff);
        }
    }
}
